package com.example.covid19.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.FaceUtil.FaceMe;
import com.example.covid19.common.Dto.RegisterDto;

import com.example.covid19.common.lang.Result;

import com.example.covid19.entity.User;
import com.example.covid19.entity.UserRole;
import com.example.covid19.entity.Userinfo;
import com.example.covid19.mapper.RechargeMapper;
import com.example.covid19.mapper.UserMapper;
import com.example.covid19.mapper.UserinfoMapper;
import com.example.covid19.service.UserDiscountService;
import com.example.covid19.service.UserRoleService;
import com.example.covid19.service.UserService;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2022-09-12
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;
    private final UserDiscountService userDiscountService;
    private final UserRoleService userRoleService;


    private final UserMapper userMapper;
    private final RechargeMapper rechargeMapper;

    private final UserinfoMapper userinfoMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    FaceMe faceMe;

    public UserController(UserService userService, UserDiscountService userDiscountService, UserRoleService userRoleService, UserMapper userMapper, RechargeMapper rechargeMapper, UserinfoMapper userinfoMapper) {
        this.userService = userService;
        this.userDiscountService = userDiscountService;
        this.userRoleService = userRoleService;
        this.userMapper = userMapper;
        this.rechargeMapper = rechargeMapper;
        this.userinfoMapper = userinfoMapper;
    }

    @RequestMapping("Facelogin")
    public Result Facelogin(@RequestPart("file") MultipartFile multipartFile) throws Exception {
        return userService.Facelogin(multipartFile);
    }


    @RequestMapping("/doLogin")
    public Result doLogin(@Param("studentid") String studentid, @Param("password") String password) {
        return userService.doLogin(studentid, password);
    }

    @RequestMapping("/logout")
    public void logout() {
        StpUtil.logout();
    }

    @PostMapping("/register")
    public Result register(@Validated @RequestBody RegisterDto registerDto) throws Exception {
        return userService.register(registerDto);

    }

    @GetMapping("/userinfo")
    public Result userinfo() {
        return Result.success(200, "ok", userinfoMapper.selectList(new QueryWrapper<Userinfo>()
                .eq("userid", Integer.parseInt(StpUtil.getLoginId().toString()))));

    }
    @PostMapping("/sendMsg")
    public void sendMessage(@RequestParam("phone") String phone) {
        userService.sendMessage(phone);
    }
    @PostMapping("/usedl")
    public Result usedl(@RequestParam("userid") Integer userid) {
        User user = userService.getOne(new QueryWrapper<User>().eq(
                "userid", userid
        ));
        user.setDeid(1);
        return Result.success(200,"成功",userMapper.updateById(user));
    }
    @PostMapping("/Ginf")
    public Result Ginf(){
        return Result.success(200,"成功",userMapper.selectList(null));
    }

    @RequestMapping("/upload")
    public Result upLoad(@RequestPart("file") MultipartFile multipartFile){
        log.info("文件上传开始");
        log.info("文件{}",multipartFile.getOriginalFilename());
        return Result.success(200,"返回的数据",ExcelReader.upload(multipartFile,User.class));
    }
    @RequestMapping("/upsave")
    public Result upsave(@RequestBody JSONArray jsonArray){
      return userService.upsave(jsonArray);
    }


}
